<?php


namespace apps\marriage\core\model;

use think\facade\Db;
use vm\com\BaseModel;

class UserChatMessage extends BaseModel
{
    const STATUS_WAIT_READ = 10;    // 未读
    const STATUS_READ = 20; // 已读

    public function getMyChatSessions($toUid)
    {
        $res = $this->field('send_uid')->where('to_uid', $toUid)->group('send_uid')->order('id', 'DESC')->select();
        $data = [];
        if($res) {
            foreach ($res as $rs) {
                $data[] = $rs->getData();
            }
            return $data;
        }
        return $data;
    }

    public function getLastChatSession($toUid, $sendUid)
    {
        $rs = $this->where('to_uid', $toUid)
                    ->where('send_uid', $sendUid)
                    ->order('id', 'DESC')
                    ->find();
        return $rs ? $rs->getData() : null;
    }

    public function getSession($uid1, $uid2)
    {
        $sql = "SELECT * FROM " . $this->getTable() . " WHERE (send_uid='$uid1' AND to_uid='$uid2') OR (send_uid='$uid2' AND to_uid='$uid1') ORDER BY id ASC";
        $res = Db::query($sql);
        return $res ? $res : [];
    }
}